﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.DAL
{
    /// <summary>
    /// 数据访问类Weight。
    /// </summary>
    public class WeightDAL
    {
        public WeightDAL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("tb_Weight", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.Weight model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_Weight(");
            strSql.Append("Years,Weight1,Weight2,Weight3,Weight4)");
            strSql.Append(" values (");
            strSql.Append("@Years,@Weight1,@Weight2,@Weight3,@Weight4)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Years", SqlDbType.Char,4),
					new SqlParameter("@Weight1", SqlDbType.Decimal,5),
					new SqlParameter("@Weight2", SqlDbType.Decimal,5),
					new SqlParameter("@Weight3", SqlDbType.Decimal,5),
					new SqlParameter("@Weight4", SqlDbType.Decimal,5)};
            parameters[0].Value = model.Years;
            parameters[1].Value = model.Weight1;
            parameters[2].Value = model.Weight2;
            parameters[3].Value = model.Weight3;
            parameters[4].Value = model.Weight4;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.Weight model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_Weight set ");
            strSql.Append("Years=@Years,");
            strSql.Append("Weight1=@Weight1,");
            strSql.Append("Weight2=@Weight2,");
            strSql.Append("Weight3=@Weight3,");
            strSql.Append("Weight4=@Weight4");
            strSql.Append(" where W_ID=@W_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@W_ID", SqlDbType.Int,4),
					new SqlParameter("@Years", SqlDbType.Char,4),
					new SqlParameter("@Weight1", SqlDbType.Decimal,5),
					new SqlParameter("@Weight2", SqlDbType.Decimal,5),
					new SqlParameter("@Weight3", SqlDbType.Decimal,5),
					new SqlParameter("@Weight4", SqlDbType.Decimal,5)};
            parameters[0].Value = model.W_ID;
            parameters[1].Value = model.Years;
            parameters[2].Value = model.Weight1;
            parameters[3].Value = model.Weight2;
            parameters[4].Value = model.Weight3;
            parameters[5].Value = model.Weight4;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string sIDs)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_Weight ");
            strSql.Append(" where W_ID in ( " + sIDs + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.Weight GetModel(int W_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 W_ID,Years,Weight1,Weight2,Weight3,Weight4 from tb_Weight ");
            strSql.Append(" where W_ID=@W_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@W_ID", SqlDbType.Int,4)};
            parameters[0].Value = W_ID;

            HsTec.Mobile.Green.Model.Weight model = new HsTec.Mobile.Green.Model.Weight();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["W_ID"].ToString() != "")
                {
                    model.W_ID = int.Parse(ds.Tables[0].Rows[0]["W_ID"].ToString());
                }
                model.Years = ds.Tables[0].Rows[0]["Years"].ToString();
                if (ds.Tables[0].Rows[0]["Weight1"].ToString() != "")
                {
                    model.Weight1 = decimal.Parse(ds.Tables[0].Rows[0]["Weight1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Weight2"].ToString() != "")
                {
                    model.Weight2 = decimal.Parse(ds.Tables[0].Rows[0]["Weight2"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Weight3"].ToString() != "")
                {
                    model.Weight3 = decimal.Parse(ds.Tables[0].Rows[0]["Weight3"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Weight4"].ToString() != "")
                {
                    model.Weight4 = decimal.Parse(ds.Tables[0].Rows[0]["Weight4"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select W_ID,Years,Weight1,Weight2,Weight3,Weight4 ");
            strSql.Append(" FROM tb_Weight ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" W_ID,Years,Weight1,Weight2,Weight3,Weight4 ");
            strSql.Append(" FROM tb_Weight ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        #endregion  成员方法
    }
}

